package org.dromara.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.OrderSalesDataBo;
import org.dromara.system.domain.vo.OrderSalesDataVo;
import org.dromara.system.domain.OrderSalesData;
import org.dromara.system.mapper.OrderSalesDataMapper;
import org.dromara.system.service.IOrderSalesDataService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 订单销售数据信息Service业务层处理
 *
 * @author joker
 * @date 2025-05-05
 */
@RequiredArgsConstructor
@Service
public class OrderSalesDataServiceImpl implements IOrderSalesDataService {

    private final OrderSalesDataMapper baseMapper;

    /**
     * 查询订单销售数据信息
     *
     * @param id 主键
     * @return 订单销售数据信息
     */
    @Override
    public OrderSalesDataVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询订单销售数据信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 订单销售数据信息分页列表
     */
    @Override
    public TableDataInfo<OrderSalesDataVo> queryPageList(OrderSalesDataBo bo, PageQuery pageQuery) {
        QueryWrapper<OrderSalesData> lqw = buildGenTableQueryWrapper(bo);
        Page<OrderSalesDataVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的订单销售数据信息列表
     *
     * @param bo 查询条件
     * @return 订单销售数据信息列表
     */
    @Override
    public List<OrderSalesDataVo> queryList(OrderSalesDataBo bo) {
        LambdaQueryWrapper<OrderSalesData> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private QueryWrapper<OrderSalesData> buildGenTableQueryWrapper(OrderSalesDataBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<OrderSalesData> wrapper = Wrappers.query();
        wrapper
            .like(StringUtils.isNotEmpty(bo.getSystemOrderNumber()), "system_order_number", bo.getSystemOrderNumber())
            .eq(StringUtils.isNotEmpty(bo.getOrderStatus()), "order_status", bo.getOrderStatus())
            .eq(StringUtils.isNotBlank(bo.getOrderSource()), "order_source", bo.getOrderSource())
            .between(params.get("beginTime") != null && params.get("endTime") != null,
                "creation_time", params.get("beginTime"), params.get("endTime"))
            .orderByDesc("creation_time");
        return wrapper;
    }

    private LambdaQueryWrapper<OrderSalesData> buildQueryWrapper(OrderSalesDataBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OrderSalesData> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(OrderSalesData::getId);
        lqw.like(StringUtils.isNotBlank(bo.getSystemOrderNumber()), OrderSalesData::getSystemOrderNumber, bo.getSystemOrderNumber());
        lqw.like(StringUtils.isNotBlank(bo.getOrderName()), OrderSalesData::getOrderName, bo.getOrderName());
        lqw.eq(StringUtils.isNotBlank(bo.getMerchant()), OrderSalesData::getMerchant, bo.getMerchant());
        lqw.like(StringUtils.isNotBlank(bo.getSupplierName()), OrderSalesData::getSupplierName, bo.getSupplierName());
        lqw.eq(StringUtils.isNotBlank(bo.getProductCode()), OrderSalesData::getProductCode, bo.getProductCode());
        lqw.eq(StringUtils.isNotBlank(bo.getCommodityCode()), OrderSalesData::getCommodityCode, bo.getCommodityCode());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderStatus()), OrderSalesData::getOrderStatus, bo.getOrderStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getStatusDescription()), OrderSalesData::getStatusDescription, bo.getStatusDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderSource()), OrderSalesData::getOrderSource, bo.getOrderSource());
        lqw.like(StringUtils.isNotBlank(bo.getIdCardName()), OrderSalesData::getIdCardName, bo.getIdCardName());
        lqw.eq(StringUtils.isNotBlank(bo.getIdCardNumber()), OrderSalesData::getIdCardNumber, bo.getIdCardNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getRecipient()), OrderSalesData::getRecipient, bo.getRecipient());
        lqw.eq(StringUtils.isNotBlank(bo.getContactPhoneNumber()), OrderSalesData::getContactPhoneNumber, bo.getContactPhoneNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getProvince()), OrderSalesData::getProvince, bo.getProvince());
        lqw.eq(StringUtils.isNotBlank(bo.getCity()), OrderSalesData::getCity, bo.getCity());
        lqw.eq(StringUtils.isNotBlank(bo.getDistrictCounty()), OrderSalesData::getDistrictCounty, bo.getDistrictCounty());
        lqw.eq(StringUtils.isNotBlank(bo.getShippingAddress()), OrderSalesData::getShippingAddress, bo.getShippingAddress());
        lqw.like(StringUtils.isNotBlank(bo.getStoreName()), OrderSalesData::getStoreName, bo.getStoreName());
        lqw.eq(StringUtils.isNotBlank(bo.getExternalOrderNumber()), OrderSalesData::getExternalOrderNumber, bo.getExternalOrderNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getExtendedOrderNumber()), OrderSalesData::getExtendedOrderNumber, bo.getExtendedOrderNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getTransferOrderMark()), OrderSalesData::getTransferOrderMark, bo.getTransferOrderMark());
        lqw.eq(bo.getTransferOrderTime() != null, OrderSalesData::getTransferOrderTime, bo.getTransferOrderTime());
        lqw.eq(StringUtils.isNotBlank(bo.getLogisticsOrderNumber()), OrderSalesData::getLogisticsOrderNumber, bo.getLogisticsOrderNumber());
        lqw.like(StringUtils.isNotBlank(bo.getLogisticsName()), OrderSalesData::getLogisticsName, bo.getLogisticsName());
        lqw.eq(StringUtils.isNotBlank(bo.getActivationNumber()), OrderSalesData::getActivationNumber, bo.getActivationNumber());
        lqw.eq(bo.getShippingTime() != null, OrderSalesData::getShippingTime, bo.getShippingTime());
        lqw.eq(bo.getActivationTime() != null, OrderSalesData::getActivationTime, bo.getActivationTime());
        lqw.eq(bo.getFirstRechargeTime() != null, OrderSalesData::getFirstRechargeTime, bo.getFirstRechargeTime());
        lqw.eq(bo.getCreationTime() != null, OrderSalesData::getCreationTime, bo.getCreationTime());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), OrderSalesData::getRemarks, bo.getRemarks());
        lqw.eq(bo.getLastUpdateTime() != null, OrderSalesData::getLastUpdateTime, bo.getLastUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getCommissionSettlementStatus()), OrderSalesData::getCommissionSettlementStatus, bo.getCommissionSettlementStatus());
        lqw.eq(bo.getCommissionSettlementAmount() != null, OrderSalesData::getCommissionSettlementAmount, bo.getCommissionSettlementAmount());
        lqw.like(StringUtils.isNotBlank(bo.getTopLevelSupplierName()), OrderSalesData::getTopLevelSupplierName, bo.getTopLevelSupplierName());
        lqw.eq(StringUtils.isNotBlank(bo.getLiveStreamerId()), OrderSalesData::getLiveStreamerId, bo.getLiveStreamerId());
        lqw.like(StringUtils.isNotBlank(bo.getLiveStreamerName()), OrderSalesData::getLiveStreamerName, bo.getLiveStreamerName());
        lqw.eq(StringUtils.isNotBlank(bo.getCommodityType()), OrderSalesData::getCommodityType, bo.getCommodityType());
        lqw.eq(StringUtils.isNotBlank(bo.getCardOpeningNumberPlaceOfOrigin()), OrderSalesData::getCardOpeningNumberPlaceOfOrigin, bo.getCardOpeningNumberPlaceOfOrigin());
        return lqw;
    }

    /**
     * 新增订单销售数据信息
     *
     * @param bo 订单销售数据信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(OrderSalesDataBo bo) {
        OrderSalesData add = MapstructUtils.convert(bo, OrderSalesData.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订单销售数据信息
     *
     * @param bo 订单销售数据信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(OrderSalesDataBo bo) {
        OrderSalesData update = MapstructUtils.convert(bo, OrderSalesData.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OrderSalesData entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除订单销售数据信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    /**
     * 根据订单创建时间查询订单
     *
     * @param creationTime
     * @return
     */
    @Override
    public OrderSalesDataVo queryOrderByOrderNoAndTime(String systemOrderNumber, String creationTime) {
        return baseMapper.selectVoOne(
            new LambdaQueryWrapper<OrderSalesData>()
                .eq(OrderSalesData::getSystemOrderNumber, systemOrderNumber)
                .likeRight(OrderSalesData::getCreationTime, creationTime));
    }

    @Override
    public Boolean updateOrderByOrderNoAndTime(OrderSalesDataBo orderSalesData, String creationTime) {
        OrderSalesData data = BeanUtil.toBean(orderSalesData, OrderSalesData.class);
        return baseMapper.update(data,
            new LambdaQueryWrapper<OrderSalesData>()
                .eq(OrderSalesData::getSystemOrderNumber, orderSalesData.getSystemOrderNumber())
                .likeRight(OrderSalesData::getCreationTime, creationTime)) > 0;
    }

}
